package gob.osinerg.emergencia.action;

import gob.osinerg.emergencia.bean.AccidenteLeve;
import gob.osinerg.emergencia.bean.Actividad;
import gob.osinerg.emergencia.bean.ActividadBean;
import gob.osinerg.emergencia.bean.EmergenciaOperativaPreliminar;
import gob.osinerg.emergencia.bean.FormularioEmergenciaUnmay;
import gob.osinerg.emergencia.bean.UnidadOperativaBean;
import gob.osinerg.emergencia.bean.UsuarioBean;
import gob.osinerg.emergencia.form.AccidenteLeveForm;
import gob.osinerg.emergencia.form.EmergenciaOperativaPreliminarForm;
import gob.osinerg.emergencia.service.EmergenciaOperativaPreliminarService;
import gob.osinerg.emergencia.service.GenericService;
import gob.osinerg.emergencia.util.Report;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.util.ResourceUtils;

//import com.sun.rsasign.v;

public class EmergenciaOperativaPreliminarAction extends GenericAction {

	private Log log = LogFactory.getLog(EmergenciaOperativaPreliminarAction.class);

	private EmergenciaOperativaPreliminarService service;
	private GenericService genericService;

	public ActionForward inicio(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws java.lang.Exception {

		EmergenciaOperativaPreliminarForm form = (EmergenciaOperativaPreliminarForm) actionForm;
		form.setBean(new EmergenciaOperativaPreliminar());
		return actionMapping.findForward("inicio");
	}
	
	public ActionForward doSave(ActionMapping actionMapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) throws java.lang.Exception {

		EmergenciaOperativaPreliminarForm form = (EmergenciaOperativaPreliminarForm) actionForm;

		String message = "messages.insert.successful";
		try {
			EmergenciaOperativaPreliminar bean = (EmergenciaOperativaPreliminar) form.getBean();
			FormularioEmergenciaUnmay beanform = new FormularioEmergenciaUnmay();
			HttpSession session = request.getSession();
			UnidadOperativaBean unidadOperativa = (UnidadOperativaBean) session
					.getAttribute("UnidadOperativaBean");
			UsuarioBean usuario = (UsuarioBean) session.getAttribute("UsuarioBean");
			ActividadBean actividad=(ActividadBean)session.getAttribute("ActividadBean");
			Long idUnidadOperativa = Long.valueOf(unidadOperativa.getCodigo());
			//seteando valores formulario emergencia unmay
			beanform.setUniopeId(idUnidadOperativa);
			beanform.setCdgoOsinerg(unidadOperativa.getCodigoOsinerg());
			beanform.setRegistroDgh(bean.getRegistroDgh());
			beanform.setFrmlarioId(new Long(3));
			beanform.setTpoEmrgncia("EOP");
			beanform.setEstado("GE");
			beanform.setMdioInfrmtvo("SEM");
			beanform.setActiviId(new Long(actividad.getCodigo()));
			beanform.setFchaEmrgncia(bean.getFechaSiniestro());
			beanform.setUsrioCrdor(usuario.getCodigoLogin());
			beanform.setFchaCrcion(new Date());
			bean.setEstdo("PRE");
			bean.setUsrioCrdor(usuario.getCodigoLogin());
			bean.setFchaCrcion(new Date());

			service.save(bean,beanform);
			sendSuccessfulMessage(request, message);
		} catch (Exception ex) {
			log.error(ex.getMessage());
			ex.printStackTrace();
			message = "errors.update.failed.finalParam";
			throw new Exception(ex.getMessage());
		}

		return actionMapping.findForward("inicio");
	}
	
	 public ActionForward exportar(ActionMapping mapping, ActionForm actionForm,
	            HttpServletRequest request, HttpServletResponse response) throws Exception {
	    	
		 EmergenciaOperativaPreliminarForm form = (EmergenciaOperativaPreliminarForm)actionForm;

	    	try{
	            request.setAttribute("actionReport","reporteEmergenciaOperativaPreliminar.do");
	            HttpSession session = request.getSession();
	    		String eopid = request.getParameter("eopid");
	    		System.out.println("eopid: "+eopid);
	            Map parameters = new HashMap();
	            parameters.put("ID", eopid);
	            parameters.put("IMAGE_LOGO", request.getRequestURL().substring(0, request.getRequestURL().lastIndexOf("/"))+"/images/logo_home.jpg");

	            File archivo = ResourceUtils.getFile("classpath:gob/osinerg/emergencia/resource/jrxml/ReporteEmergenciaOperativaPreliminar.jrxml");

	    		Report report = new Report();
	    		report.setJasperReport(archivo);
	    		report.setParameter(parameters);
	    		report.setConnection(genericService.getConnection());
    			report.PDF(request, response);   		
  		
	    	}catch(Exception ex){
	    		log.error(ex.getMessage());
	    	}
	        return null;
	    }	
	
	
	public void setGenericService(GenericService genericService) {
		this.genericService = genericService;
	}

	public void setService(EmergenciaOperativaPreliminarService service) {
		this.service = service;
	}

}
